package com.qgzj;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * @Description:
 * @Author: huixiangyin
 * @CreateTime: 2018/8/27 14:52
 */
public class GeneratorCode {

    @Test
    public void generateCode() {
        String packageName = "com.qgzj";
        //修改替换成你需要的表名，多个表名传数组
        generateByTables(packageName, new String[]{"user_wechat_info"});
    }

    private void generateByTables(String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://xxx:3306/xxx?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&useSSL=false";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("xxx")
                .setPassword("xxx")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
//                .setTablePrefix("t_")
                .setInclude(tableNames);
        config.setActiveRecord(false)
                .setAuthor("yang").setOutputDir("xxx/src/main/java")
                .setFileOverride(true)
                .setEnableCache(false)
                .setActiveRecord(true);
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                                .setMapper("mapper")
                ).execute();
    }
}
